package com.tianzeculture.stagebao.activity.home.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.IdRes;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.home.adapter.WorkTagAdapter;
import com.tianzeculture.stagebao.dialog.DateDialog;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.view.MyGridView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * Created by Administrator on 2018/6/5 0005.
 * 人才筛选
 */

public class TalentSearchPopWindow extends PopupWindow implements View.OnClickListener {
    private Activity mContext;
    private final LayoutInflater inflater;
    private View searchView;
    private final Display display;
    private final WindowManager windowManager;
    private TalentSearchListener talentSearchListener;

    EditText tvAge1, tvAge2, tvHeight1, tvHeight2, tvWeight1, tvWeight2;
    Button btnReset, btnSure;
    RadioGroup radioGroup;
    boolean isStartPrice = true;
    int sex;

    public TalentSearchPopWindow(Activity context) {
        this.mContext = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        searchView = inflater.inflate(R.layout.popwindow_talent_search, null);

        this.setContentView(searchView);
        //设置CityPickerPopWindow弹出窗体的宽
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.setWidth(RelativeLayout.LayoutParams.FILL_PARENT);
        //设置CityPickerPopWindow弹出窗体的高
//        this.setHeight((int) (display.getHeight() * 0.35));
        this.setHeight((int) (RelativeLayout.LayoutParams.WRAP_CONTENT));
        //设置CityPickerPopWindow弹出窗体可点击
        this.setFocusable(true);
        //设置CityPickerPopWindow可触摸
        this.setTouchable(true);
        //设置CityPickerPopWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x9000000);
        //设置CityPickerPopWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        setUpListener(searchView);
    }


    public void setUpListener(View searchView) {
        radioGroup = searchView.findViewById(R.id.radioGroup);
        tvAge1 = searchView.findViewById(R.id.tvAge1);
        tvAge2 = searchView.findViewById(R.id.tvAge2);
        tvHeight1 = searchView.findViewById(R.id.tvHeight1);
        tvHeight2 = searchView.findViewById(R.id.tvHeight2);
        tvWeight1 = searchView.findViewById(R.id.tvWeight1);
        tvWeight2 = searchView.findViewById(R.id.tvWeight2);
        btnSure = searchView.findViewById(R.id.btnSure);
        btnReset = searchView.findViewById(R.id.btnReset);
        btnSure.setOnClickListener(this);
        btnReset.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.rbBoy:
                        sex = 1;
                        break;
                    case R.id.rbGirl:
                        sex = 0;
                        break;
                    case R.id.rbAll:
                        sex = 2;
                        break;
                }
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnSure:
                String age1 = tvAge1.getText().toString().trim();
                String age2 = tvAge2.getText().toString().trim();
                String height1 = tvHeight1.getText().toString().trim();
                String height2 = tvHeight2.getText().toString().trim();
                String weight1 = tvWeight1.getText().toString().trim();
                String weight2 = tvWeight2.getText().toString().trim();
                try {
                    if (Integer.parseInt(age2) < Integer.parseInt(age1)) {
                        ToastUtil.showShort(mContext, "请确保年龄正确");
                        return;
                    }
                    if (Integer.parseInt(height2) < Integer.parseInt(height1)) {
                        ToastUtil.showShort(mContext, "请确保身高正确");
                        return;
                    }
                    if (Integer.parseInt(weight2) < Integer.parseInt(weight1)) {
                        ToastUtil.showShort(mContext, "请确保体重正确");
                        return;
                    }
                } catch (Exception e) {
                    String age = age1 + "-" + age2;
                    String height = height1 + "-" + height2;
                    String weight = weight1 + "-" + weight2;
                    talentSearchListener.pickTalentSearchValue(age, height, weight, sex);
                    dismiss();
                }
                String age = age1 + "-" + age2;
                String height = height1 + "-" + height2;
                String weight = weight1 + "-" + weight2;
                talentSearchListener.pickTalentSearchValue(age, height, weight, sex);
                dismiss();
                break;
            case R.id.btnReset:
                radioGroup.check(R.id.rbAll);
                tvAge1.setText("");
                tvAge2.setText("");
                tvHeight1.setText("");
                tvHeight2.setText("");
                tvWeight1.setText("");
                tvWeight2.setText("");
                break;
        }
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }


    public interface TalentSearchListener {
        void pickTalentSearchValue(String age, String height, String weight, int sex);

        void talentSearchCancel();
    }

    public void setTalentSearchListener(TalentSearchListener listener) {
        this.talentSearchListener = listener;
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        talentSearchListener.talentSearchCancel();
    }
}
